package com.autumn.platform.core.support.model;

import java.util.List;

import org.springframework.cache.Cache;

import com.autumn.platform.core.support.model.form.Form;
import com.autumn.platform.core.support.model.form.loader.IFormLoader;
import com.autumn.platform.core.support.model.grid.Grid;
import com.autumn.platform.core.support.model.grid.loader.IGridLoader;

public class ModelManager {

    private static Cache cache;

    private static List<IGridLoader> gridLoaders;

    private static List<IFormLoader> formLoaders;

    public static Cache getCache() {
        return cache;
    }

    public static void setCache(Cache cache) {
        ModelManager.cache = cache;
    }

    public static List<IGridLoader> getGridLoaders() {
        return gridLoaders;
    }

    public static void setGridLoaders(List<IGridLoader> gridLoaders) {
        ModelManager.gridLoaders = gridLoaders;
    }

    public static List<IFormLoader> getFormLoaders() {
        return formLoaders;
    }

    public static void setFormLoaders(List<IFormLoader> formLoaders) {
        ModelManager.formLoaders = formLoaders;
    }

    /**
     * 刷新模型
     * 
     * @param modelId
     * @return
     */
    public static Grid refreshGridModel(String modelId) {
        removeGridModel(modelId);
        return getGridModel(modelId);
    }

    /**
     * 移除模型
     * 
     * @param modelId
     * @return
     */
    public static void removeGridModel(String modelId) {
        if (null != cache) {
            cache.evict(getCacheGridModelKey(modelId));
        }
    }

    /**
     * 根据ID构建模型
     * 
     * @param modelId
     * @return
     */
    public static Grid getGridModel(String modelId) {
        if (null == cache) {
            return null;
        }
        String key = getCacheGridModelKey(modelId);
        Grid grid = cache.get(key, Grid.class);
        if (null == grid) {
            List<IGridLoader> loaders = getGridLoaders();
            if (null != loaders && !loaders.isEmpty()) {
                synchronized (cache) {
                    grid = cache.get(key, Grid.class);
                    if (null == grid) {
                        for (IGridLoader loader : loaders) {
                            try {
                                grid = loader.loadGridModel(modelId);
                            } catch (Exception e) {
                            }
                            if (null != grid) {
                                cache.put(key, grid);
                                return grid;
                            }
                        }
                    }
                }
            }
        }
        return grid;
    }

    /**
     * 刷新模型
     * 
     * @param modelId
     * @return
     */
    public static Form refreshFormModel(String modelId) {
        removeGridModel(modelId);
        return getFormModel(modelId);
    }

    /**
     * 移除模型
     * 
     * @param modelId
     * @return
     */
    public static void removeFormModel(String modelId) {
        if (null != cache) {
            cache.evict(getCacheFormModelKey(modelId));
        }
    }

    /**
     * 根据ID构建模型
     * 
     * @param modelId
     * @return
     */
    public static Form getFormModel(String modelId) {
        if (null == cache) {
            return null;
        }
        String key = getCacheFormModelKey(modelId);
        Form form = cache.get(key, Form.class);
        if (null == form) {
            List<IFormLoader> loaders = getFormLoaders();
            if (null != loaders && !loaders.isEmpty()) {
                synchronized (cache) {
                    form = cache.get(key, Form.class);
                    if (null == form) {
                        for (IFormLoader loader : loaders) {
                            try {
                                form = loader.loadFormModel(modelId);
                            } catch (Exception e) {
                            }
                            if (null != form) {
                                cache.put(key, form);
                                return form;
                            }
                        }
                    }
                }
            }
        }
        return form;
    }

    private static String getCacheGridModelKey(String modelId) {
        return "GridModelId-" + modelId;
    }

    private static String getCacheFormModelKey(String modelId) {
        return "FormModelId-" + modelId;
    }
}
